import 'package:flutter/material.dart';
import 'package:flutter_common/widget/funAppBar.dart';
import 'package:flutter_common/config/config.dart';
import 'package:flutter_common/widget/toast.dart';
import 'package:flutter_common/net/http.dart';
import 'package:flutter_common/widget/loading.dart';
import 'package:flutter_fun_app/model/rangeListModel.dart';
import 'package:flutter_common/widget/msgDialog.dart';
import 'package:flutter_common/widget/loadingAnim.dart';

class RangeListPage extends StatefulWidget {
  String title;
  int type;

  RangeListPage({this.title, this.type});

  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return RangeListState(title: title, type: type);
  }
}

class RangeListState extends State<RangeListPage> {
  String title;
  int type;

  RangeListState({this.title, this.type});

  List<Result> rangeList = List();

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    getRangeList();
  }

  Future<void> getRangeList() async {
    var url = 'https://api.apiopen.top/musicRankingsDetails';
    var data = {'type': type};
    HttpUtil.get(url, data: data, success: (data) {
      RangeListModel rangeListModel = RangeListModel.fromJson(data);
      if (rangeListModel.code == 200) {
        setState(() {
          rangeList = rangeListModel.result;
        });
      } else {
        ToastUtil.show(rangeListModel.message);
      }
    }, then: () {});
  }

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: FunAppBar(
        title: title,
      ),
      body: rangeList.length == 0
          ? LoadingAnim.getProgressAnim()
          : ListView.builder(
              itemBuilder: (BuildContext context, int index) {
                return _itemBuilder(index);
              },
              itemCount: rangeList.length,
            ),
    );
  }

  Widget _itemBuilder(int index) {
    Result result = rangeList[index];
    return Column(
      children: <Widget>[
        ListTile(
          onTap: () {
            MsgDialog.show(context, content: result.title);
          },
          title: Text(result.title),
          subtitle: Text(result.author),
          leading: ClipOval(
            child: Image.network(
              result.picSmall,
              width: 50,
              height: 50,
            ),
          ),
          trailing: IconButton(
            icon: Icon(Icons.play_circle_outline),
            color: Config.titleBarColor,
            onPressed: () {},
          ),
        ),
        Divider(
          height: 2,
        )
      ],
    );
  }
}
